﻿using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace EmoneyWeiXin.Controllers
{
    public class JiaQQController : Controller
    {

        // GET: /JiaQQ/
        const string qqkeymmKey = "QQKey_mm_key";
        public static object objQQkey = new object();
        public static string currentKey = "";
        static MemcachedTools1 mmtoos = new MemcachedTools1();
        public ActionResult get99qq()
        {

            string qqKey = mmtoos.Get<string>(qqkeymmKey);
            if (qqKey == null)
            {
                lock (objQQkey)
                {
                    qqKey = QQKeyBLL.GetQiyongKey().Trim();
                    SetQQkeyToMmcache(qqKey);
                }
            }
            string url = Config.JiaQQQunUrl;
            return Redirect(string.Format(url, qqKey));

            //if (string.IsNullOrEmpty(currentKey))
            //{
            //    lock (objQQkey)
            //    {
            //        currentKey = QQKeyBLL.GetQiyongKey().Trim();
            //    }
            //}

            //string url = Config.JiaQQQunUrl;
            //return Redirect(string.Format(url, currentKey));
        }

        public ActionResult SetQQKeyPage()
        {
            string password = Request["password"];
            if (string.IsNullOrEmpty(password))
            {
                return Content("请带上密码参数！");
            }
            else
            {
                if (password != System.Configuration.ConfigurationManager.AppSettings["huanQunMima"])
                {
                    return Content("密码错误！");
                }
            }
            return View();
        }

        private void SetQQkeyToMmcache(string qqkey)
        {
            mmtoos.Add(qqkeymmKey, qqkey, 24 * 5, 0, 0);
        }

        public ActionResult AddQQKey()
        {
            string name = Request["QQName"];
            string key = Request["QQkey"];
            try
            {
                QQKeyBLL.Insert(name, key);
                return Content("添加正确");
            }
            catch (Exception ex)
            {
                return Content("添加失败:" + ex.Message);
            }
        }

        public ActionResult Delete()
        {
            string key = Request["qqKey"];
            try
            {
                QQKeyBLL.Delete(key);
                return Content("删除成功");
            }
            catch (Exception)
            {
                return Content("删除失败");
            }
        }

        public ActionResult SetStatusTO1()
        {
            string key = Request["qqKey"];
            try
            {
                QQKeyBLL.SetStatusTo1(key);
                SetQQkeyToMmcache(key);
                return Content("启用成功");

            }
            catch (Exception ex)
            {
                return Content("启用失败:" + ex.Message);
            }
        }

        private int pageSize = 15;
        public ActionResult GetDataByPage()
        {
            string pageIndex = Request["pageIndex"];
            try
            {
                int totalCount = 0;
                DataTable dt = QQKeyBLL.SelectByPage(pageSize, int.Parse(pageIndex), out totalCount);
                string jsons = JsonTools.ToJson(new { list = dt, totalCount = totalCount });
                return Content(jsons);
            }
            catch (Exception)
            {
                return Content("");
            }
        }


    }
}
